import { constantRoutes, asyncRoutes } from '@/router'

export default {
  namespaced: true,
  state() {
    return {
      routes: constantRoutes // 默认静态路由表
    }
  },
  mutations: {
    // otherRoutes登录成功后, 需要添加的新路由
    setRoutes(state, otherRoutes) {
    // 静态路由基础上, 累加其他权限路由
      state.routes = [...constantRoutes, ...otherRoutes]
    }
  },
  actions: {
    async filterRoutes({ commit }, menus) {
      console.log(asyncRoutes, menus)
      /**
       * 过滤出我们的路由
       * 1. 调用mutations 添加到vuex里（静态和动态拼接）
       * 2. return返回  就可以拿到数据 addRoutes(过滤后的路由)
       */
      const otherRoutes = asyncRoutes.filter(item => {
        if (menus.includes(item.children[0].name)) {
          return true
        } else {
          return false
        }
      })
      commit('setRoutes', otherRoutes)
      console.log('过滤', otherRoutes)
      return otherRoutes
    }
  }
}
